<template>
  <Panel>
    <template #title>商品列表</template>
    <template #content>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="商品ID">
                <span>{{ props.row.id }}</span>
              </el-form-item>
              <el-form-item label="所属分类">
                <span>{{ props.row.category }}</span>
              </el-form-item>
              <el-form-item label="商品图片">
                <img
                  v-if="props.row.imgUrl"
                  :src="
                    'http://127.0.0.1:5000/upload/imgs/goods_img/' +
                      props.row.imgUrl
                  "
                  alt=""
                />
              </el-form-item>
              <el-form-item label="商品评价">
                <span>{{ props.row.rating }}</span>
              </el-form-item>
              <el-form-item label="商品描述">
                <span>{{ props.row.goodsDesc }}</span>
              </el-form-item>
              <el-form-item label="商品名称">
                <span>{{ props.row.name }}</span>
              </el-form-item>
              <el-form-item label="商品价格">
                <span>{{ props.row.price }}</span>
              </el-form-item>
              <el-form-item label="创建时间">
                <span>{{ props.row.ctime | dataForat }}</span>
              </el-form-item>
              <el-form-item label="商品销量">
                <span>{{ props.row.sellCount }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="商品名称" prop="name"> </el-table-column>
        <el-table-column label="所属分类" prop="category"> </el-table-column>
        <el-table-column label="商品价格" prop="price"> </el-table-column>
        <el-table-column label="商品图片">
          <template v-slot="{ row }">
            <img
              :src="'http://127.0.0.1:5000/upload/imgs/goods_img/' + row.imgUrl"
              alt=""
            />
          </template>
        </el-table-column>
        <el-table-column label="商品描述" prop="goodsDesc"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button v-waves size="mini" @click="handleEdit(row)"
              >编辑</el-button
            >
            <el-button v-waves size="mini" type="danger" @click="delGoods(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <Paging :pageData="paging" @pagingIncident="getlist" />
    </template>
  </Panel>
</template>

<script>
import { listGoods, delGoods } from '@/api/good'
import { okBox } from '@/utils/hint'
import local from '@/utils/local'
export default {
  data() {
    return {
      tableData: [],
      paging: {
        currentPage: 1,
        pageSize: 4,
        total: 0
      }
    }
  },

  created() {
    this.getlist()
  },
  methods: {
    async getlist() {
      const { total, data } = await listGoods(this.paging)
      this.paging.total = total
      this.tableData = data
    },
    /* 商品修改 */
    handleEdit(row) {
      local.set('goods', JSON.stringify(row))
      this.$router.push(`/goods/edit?${row.id}`)
    },
    /* 商品删除 */
    delGoods(row) {
      okBox('删除该商品').then(async () => {
        await delGoods({ id: row.id })
        this.getlist()
      })
    }
  }
}
</script>

<style lang="less" scoped>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
img {
  width: 50px;
  height: 50px;
}
</style>
